<?php
header('Content-Type: text/html; charset=UTF-8');
include('./includes/magic_quotes.inc'); //we need this to disable idiotic magic quotes
require("./includes/config.inc"); /* this is a config file that new users/installations need to edit */
require("./includes/variables.inc"); /* these are special variables and _GET/_POST conversions */
// definitions of important functions used throughout ccramp
include ('./includes/libccramp.inc');
//
//
//
// Temporary hack. 
//if ($_SESSION[$usr]) session_register($usr);
//if ($_SESSION[$U_id]) session_register($U_id);
//if ($_SESSION[$U_]) session_register($U_);
//if ($_SESSION[$rights]) 
//session_register($rights);
$U_rights = $U_[rights] = $_SESSION[rights];
//
//
//
//
//
//

require_once('./includes/auth.inc'); // authentication

/* if we just logged out, we've got to forget about what we were doing, and head back to the login page */

/*
if ($do === "logout") {
	include ("./includes/logout.inc");
	include('login.php');
}

// check for username,password, and create session variables
if(!$U_id && $do === 'login' && $_POST['username']) {
	$encrypted_password = (md5(strtolower(trim($_POST['username'])) . strtolower(trim($_POST['password']))));
	$U_id = mysql_result(mysql_query("SELECT `id` FROM `users` WHERE `username`='".mysql_real_escape_string($_POST['username'])."' AND `password`='".$encrypted_password."' LIMIT 1"), 0);
	$_SESSION['U_id'] = $U_id;
	mysql_query("INSERT INTO `logout` SET `login`='".mysql_real_escape_string($nowstamp)."',`user`='".mysql_real_escape_string($U_id)."'");
}
$U_id = $_SESSION['U_id'];

// is someone logged in?
if ($U_id) {
   echo('<h1>whoo doggey!</h1>');
	$U_ = mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `id`='".mysql_real_escape_string($U_id)."' LIMIT 1 "));
	if (!$cat) $cat = $U_[category];  //give the user their default category if they don't already have one
/*
$quer2 = mysql_query("SELECT `id`,`username`,`password`,`rights`,`category`,`name` FROM `users` WHERE `username`='$_user' AND `password`='$_password'");
	if (mysql_num_rows($quer2)==0) {
		session_destroy();//kills the session if user not in the database
		die("User authentication failed, click <a href=\"index.php\">here</a> to login again.");//this stops the page from displaying to unauthorised users
	} else {
		if (!isset($cat)) { // if we haven't reset the user's category, the default should be perfect
			$cat = mysql_result($quer2,0,"category");
			if (!$cat) $cat = 'all'; // if the user has no set category, set category to 'all'
		}
		// get data from the database, and load make 'em into session variables
		$_SESSION['_user'] = mysql_result($quer2,0,"username");
		$_SESSION['U_rights'] = mysql_result ($quer2,0,"rights");
		$U_rights = $_SESSION['U_rights'];
		$_SESSION['_NAME'] = mysql_result($quer2,0,"name");
 //} else {
		$quer2 = mysql_query("SELECT * FROM `users` WHERE `username`='$_user' AND `password`='$_password'");
		if (mysql_num_rows($quer2)==0) {
			session_destroy(); //kill session if user not in the database
			die("User authentication failed, click <a href=\"index.php\">here</a> to login again.");  //die page die
		}
	}
	$sql = "SELECT `username`,`password`,`category`,`rights` FROM `users` WHERE `username`='$_user' LIMIT 1";
	$result = mysql_query($sql);
	$row = mysql_fetch_row($result);
	if (!$row) {
		echo ('you\'re not even a dj here, you fool!');
	} else if ($_password != $row[1]) {
		echo ('whoa, there cowboy!  Wrong password.');
	} else {
		mysql_free_result($result);


	if ($do === 'login'){
		if ($U_[rights] > 5){
//			include ('./admin/integrity.php');
		}
		if ($row[2]) $cat = $row[2];
	}
*/
	
//<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
//<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
?>
<html>
<head>
<script language="javascript" type="text/javascript">
function getElementsByClassName (searchClass, node, tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

// function for switching one class to another class
// requirements: getElementsByClassName()
function switchElementClassName (inClass, outClass) {
   var elementClasses = getElementsByClassName(inClass);
   for (var i = 0; i < elementClasses.length; i++) {
      elementClasses[i].className = outClass;
   }
}

function updCatFilename (catFilename) {
   var catFilename = catFilename.toLowerCase();
   document.getElementById('catFilename').innerHTML = '<input type="text" name="catFilename" value="' + catFilename + ' />';
}
function updEvtFilename (evtFilename) {
   var evtFilename = evtFilename.toLowerCase();
   document.getElementById('evtFilename').innerHTML = '<input type="text" name="evtFilename" value="' + evtFilename + ' />';
}

</script>

<script language="javascript" type="text/javascript" src="./includes/javascript/libccramp.js"></script>

<!-- // begin draglist includes -->
<link rel="STYLESHEET" type="text/css" href="./includes/draglist/assets/default.css">
<script language="JavaScript" src="./includes/draglist/assets/dom-drag.js" type="text/javascript"></script>
<script language="JavaScript" src="./includes/draglist/assets/draglist.js" type="text/javascript"></script>
<!-- end draglist includes -->

<!-- this is not necessary, it is for the layout of this page itself; not transmenus. -->
<link rel="stylesheet" type="text/css" href="./themes/<?=$theme?>/transmenu_styles.css" />


<!-- begin crap to allow IE to render the otherwise pure-css dropdown menus -->
<script type="text/javascript">
startList = function() {
	if (document.all && document.getElementById) {
		navRoot = document.getElementById("dmenu");
		for (i=0; i < navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}
window.onload=startList;
//-->
</script>
<!-- end menu crap for IE rendering -->


<script language="javascript" type="text/javascript">
onload = function MasterInit() {
   init();
   <?php  //this is for setting the proper focus based on whatever we are looking at
		if ($mode === 'traffic' ) echo('document.AirplayAdd.slot.focus();');
	?>
}
		function init() {
		}
</script>


<!--//begin ajax crap code 1 of 2 -->
<script type="text/javascript" language="Javascript">
function createXMLHttpRequest() {
   var ua; //this variable is for the user agent string for the browser
   
   if (window.XMLHttpRequest) {
      try {
        ua = new XMLHttpRequest();
      } catch(e) {
        ua = false;
      }
   } else if(window.ActiveXObject) {
      try {
         ua = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        ua = false;
      }
   }
   return ua;
}
</script>
<!--//end ajax crap code 1 of 2 -->

<script type="text/javascript" language="Javascript">
var req = createXMLHttpRequest();

// this function is for labeling songs as requests in traffic mode
function setRequest(airId) {
  if (document.getElementById('request[' + airId + ']').checked == true) {
     req.open('get', './includes/ajax_func.php?do=setRequest&id=' + airId);
     req.onreadystatechange = handleResponse;
     req.send(null);
  } else if (document.getElementById('request[' + airId + ']').checked == false) {
     req.open('get', './includes/ajax_func.php?do=unsetRequest&id=' + airId);
     req.onreadystatechange = handleResponse;
     req.send(null);
  }
}

// 
function trafDismiss(trafId) {
   if (trafId == null) {
      alert('Sorry, you cannot dismiss this event.');
   } else {
      if (confirm('Dismissing this event means that you cannot log it later.  Are you sure you want to do this?')) {
         req.open('get', './includes/ajax_func.php?do=trafDismiss&trafId=' + trafId);
         req.onreadystatechange = handleResponse;
         req.send(null);
      }
   }
}

//this function is for deleting CD data from the library, including their tracks; it does not yet trigger file removal
function delAlbum(albID) {
   if (confirm('Are you sure you want to remove this album and all its tracks from the database?  This cannot be undone!')) {
//      alert('./includes/ajax_func.php?do=delAlbum&albId=' + albID);
      req.open('get', './includes/ajax_func.php?do=delAlbum&albId=' + albID);
      req.onreadystatechange = handleResponse;
      req.send(null);
   }
}

function pickAlb (albId, usrId) {
   req.open('get', './includes/ajax_func.php?do=pickAlb&albId=' + albId + '&usrId=' + usrId);
//   alert(usrId + albId);
   req.onreadystatechange = handleResponse;
   req.send(null);
}

function expandAir (rowNum, airId) {
   req.open('get', './includes/ajax_func.php?do=expandAir&rowNum=' + rowNum + '&airId=' + airId);
   req.onreadystatechange = handleResponse;
   req.send(null);
}

function collapseAir (rowNum, airId) {
   req.open('get', './includes/ajax_func.php?do=collapseAir&rowNum=' + rowNum + '&airId=' + airId);
   req.onreadystatechange = handleResponse;
   req.send(null);
}

function showAirExport (outputType) {
   var showAirExportUrl = './includes/ajax_func.php?do=showAirExport';
   if (outputType) {
      req.open('get', showAirExportUrl + '&outputType=' + outputType);
   } else {
      req.open('get', showAirExportUrl);
   }
   req.onreadystatechange = handleResponse;
   req.send(null);
}

function rmTrk (trkId) {
   if (confirm('Are you sure you want to remove this track from the database?  This cannot be undone!')) {
      req.open('get', './includes/ajax_func.php?do=rmTrk&trkId=' + trkId);
      req.onreadystatechange = handleResponse;
      req.send(null);
   } 
}

function albSetMulti (albId) {
   if (document.getElementById('multi').value) {
      req.open ('get', './includes/ajax_func.php?do=albSetMulti&albId=' + albId);
      req.onreadystatechange = handleResponse;
      req.send(null);
   }
}

function togglePlayChunk (playId, chunkStatus) {
   alert(chunkStatus + 'this function is not yet finished!');
   req.open ('get', './includes/ajax_func.php?do=togglePlayChunk&playId=' + playId + '&chunkStatus=' + chunkStatus);
   req.onreadystatechange = handleResponse;
   req.send(null);
}


// this function updates and displays the content for the traffic event log
// it uses the handleResponse() function to send the time (not yet, I guess) to the server
function updateTrafLogger (schedId) {
   if (schedId==null) { 
      req.open('get', './includes/ajax_func.php?do=updateTrafLogger');
   } else {
      req.open('get', './includes/ajax_func.php?do=updateTrafLogger&schedId=' + schedId);
   }
   req.onreadystatechange = handleResponse;
   req.send(null);
}

function randPlayTrk (playRow) {
//   document.getElementById('playRow[0]').append(document.createElement('textarea'));
   req.open('get', './includes/ajax_func.php?do=randPlayTrk&playRow=' + playRow);
   req.onreadystatechange = handleResponse;
   req.send(null);
}

//testi = 0;
//function addrow (tableId, st) {
//   var tb= document.getElementById(tableId);
//   var tr= document.createElement('tr');
//   testi++;
//   tr.id= 'tr' + testi;
//   tb.appendChild(tr);
//   var td= document.createElement('td');
//   tr.appendChild(td);
//   td.innerHTML= 'AJAX: tr' + testi;
//   var td2= document.createElement('td');
//   tr.appendChild(td2);
//   td2.innerHTML= st;
//}

function handleResponse() {
   if(req.readyState == 4){
      var response = req.responseText;
      var update = new Array();
      if(response.indexOf('||' != -1)) {
         update = response.split('||');
         document.getElementById(update[0]).innerHTML = update[1];
//         if (document.getElementById(update[2])) {
            document.getElementById(update[2]).innerHTML = update[3];
//         }
//         if (document.getElementById(update[4])) {
            document.getElementById(update[4]).innerHTML = update[5];
//         }
      }
   }
}

//////////////////////////////////////////////////////////////////////////
//
// SECTION trafSched
// 
// functions for the trafSched mode
//
//////////////////////////////////////////////////////////////////////////

function schedEvtMulti () {
   var evtMulti = document.getElementById('trafSchedEvtMulti').value;
   if (evtMulti == 1) {
      var evtMultiContent = '<table><tr><td>Start Time </td><td><input type="text" name="evtStarttime" size="5" /> (24-hour format, required)</td>   </tr>   <tr>   <td>End Time </td>   <td><input type="text" name="evtEndtime" size="5" /> (24-hour, optional)</td>   </tr><tr><td colspan="2"><input type="hidden" name="do" value="addEvt" /><input type="submit" value="Add Event" /></td></table>';
   } else if (evtMulti == 2) {
      var evtMultiContent = '<table><tr><td>Repeat </td><td><select name="evtRepFreq"><option value="1">each hour</option><option value="2">each even hour</option><option value="3">each odd hour</option></select></td></tr><tr><td>after the hour </td><td><input type="text" name="evtTimeOffset" size="2" />minutes</td></tr><tr><td>Repeat </td><td><input type="radio" name="evtRepLength" value="1" checked="checked" /> All Day<br /><input type="radio" name="evtRepLength" value="2" />From <input type="text" size="5" name="evtRepStarttime" /> to <input type="text" size="5" name="evtRepEndtime" /><br /></td></tr><tr><td colspan="2"><input type="hidden" name="do" value="addEvt" /><input type="submit" value="Add Event" /></td></table>';
   }
   document.getElementById('schedEvtMultiDisplay').innerHTML = evtMultiContent;
}


//////////////////////////////////////////////////////////////////////////
//
// SECTION mkPlaylist
// 
// The following functions are specific to the playlist editing modes: mkPlaylist, mkManPlaylist, and mkMagicPlaylist
// 
//////////////////////////////////////////////////////////////////////////

// 
// This function unhides the playlist maker options
//
function showPlayMaker () {
   document.getElementById('playMaker').className = 'normal';
}


//////////////////////////////////////////////////////////////////////////
//
// SECTION albEdit
// 
// The following functions are specific to the album editing mode.
// 
//////////////////////////////////////////////////////////////////////////

//
// this function shows or hides the column from view
// for individual track, depending on whether or not
// the is_comp box is checked prior in the page.
// 
// dependencies: switchElementClassName ()
function toggleAlbComp () {
   if (document.getElementById('is_comp').checked) {
      switchElementClassName('trkArtistHidden','trkArtist');
      document.getElementById('artistHeading').innerHTML = 'Artist';
   } else {
      switchElementClassName ('trkArtist','trkArtistHidden');
      document.getElementById('artistHeading').innerHTML = '';
   }
}

//
// this function displays or hides extended track info
// in albEdit mode
//
function toggleTrkExtInfo () {
   if (document.getElementById('trkExtInfo').checked) {
      switchElementClassName('extInfo','extInfoHidden');
   } else {
      switchElementClassName('extInfoHidden','extInfo');
   }
}

function rateAlb (albVirtue) {
   requestQuery = './includes/ajax_func.php?do=rateAlb';
   if (albVirtue != null) requestQuery += '&albVirtue=' + albVirtue;
   req.open('get', requestQuery);
   req.onreadystatechange = handleResponse;
   req.send(null);
}

function rateTrk (trkRow, trkVirtue) {
//   alert ('rateTrk(' + trkRow + ')');
//   if (themeName == null) themeName = 'default';
   themeName = 'default';
   var imgDir = './themes/' + themeName + '/img/';
   hiddenInput = '<input type="hidden" name="trk[' + trkRow + '][virtue]" value="' + trkVirtue + '" />';
   v1_off = '<img src="' + imgDir + 'virtue1_off.png" onClick="rateTrk(' + trkRow + ', 1)" />';
   v1_on = '<img src="' + imgDir + 'virtue1_on.png" onClick="rateTrk(' + trkRow + ', 1)" />';
   v2_off = '<img src="' + imgDir + 'virtue2_off.png" onClick="rateTrk(' + trkRow + ', 2)" />';
   v2_on = '<img src="' + imgDir + 'virtue2_on.png" onClick="rateTrk(' + trkRow + ', 2)" />';
   v3_off = '<img src="' + imgDir + 'virtue3_off.png" onClick="rateTrk(' + trkRow + ', 3)" />';
   v3_on = '<img src="' + imgDir + 'virtue3_on.png" onClick="rateTrk(' + trkRow + ', 3)" />';
   vStar_off = '<img src="' + imgDir + 'star_unselected.png" ';
   vStar_on = '<img src="' + imgDir + 'star_selected.png" ';
   v4_off = vStar_off + ' onClick="rateTrk(' + trkRow + ', 4)" />';
   v4_on = vStar_on + ' onClick="rateTrk(' + trkRow + ', 4)" />';
   v5_off = vStar_off + ' onClick="rateTrk(' + trkRow + ', 5)" />';
   v5_on = vStar_on + ' onClick="rateTrk(' + trkRow + ', 5)" />';
   v6_off = vStar_off + ' onClick="rateTrk(' + trkRow + ', 6)" />';
   v6_on = vStar_on + ' onClick="rateTrk(' + trkRow + ', 6)" />';
   
   if (trkVirtue == 1) {
      var trkVirtueStr = v1_on + v2_off + v3_off + v4_off + v5_off + v6_off;
   } else if (trkVirtue == 2) {
      var trkVirtueStr = v1_off + v2_on + v3_off + v4_off + v5_off + v6_off;
   } else if (trkVirtue == 3) {
      var trkVirtueStr = v1_off + v2_off + v3_on + v4_off + v5_off + v6_off;
   } else if (trkVirtue == 4) {
      var trkVirtueStr = v1_off + v2_off + v3_off + v4_on + v5_off + v6_off;
   } else if (trkVirtue == 5) {
      var trkVirtueStr = v1_off + v2_off + v3_off + v4_on + v5_on + v6_off;
   } else if (trkVirtue == 6) {
      var trkVirtueStr = v1_off + v2_off + v3_off + v4_on + v5_on + v6_on;
   } else {
      var trkVirtueStr = v1_off + v2_off + v3_off + v4_off + v5_off + v6_off;
   }
   
   document.getElementById('rateTrk[' + trkRow + ']').innerHTML = hiddenInput + trkVirtueStr;
   
/*
   var trkId = trkRow;
   trk = 'rateTrk[' + trkId + ']';
   if (trkVirtue > 4) {
      document.getElementById(trk).innerHTML = '<input type="hidden" name="trk[' + trkId + '][virtue]" value="' + trkVirtue + '" /><img src="./themes/' + themeName + '/img/virtue1_off.png" onClick="rateTrk(0,' + trkId + ')" /><img src="./themes/' + themeName + '/img/virtue2_off.png" onClick="rateTrk(1,' + trkId + ')" /><img src="./themes/' + themeName + '/img/virtue3_off.png" onClick="rateTrk(2,' + trkId + ')" /><img src="./themes/' + themeName + '/img/star_selected.png" onClick="rateTrk(3,' + trkId + ')" /><img src="./themes/' + themeName + '/img/star_selected.png" onClick="rateTrk(4,' + trkId + ')" /><img src="./themes/' + themeName + '/img/star_selected.png" onClick="rateTrk(5,' + trkId + ')" />';
   } else if (trkVirtue == 4) {
      document.getElementById(trk).innerHTML = '<input type="hidden" name="trk[' + trkId + '][virtue]" value="' + trkVirtue + '" /><img src="./themes/' + themeName + '/img/virtue1_off.png" onClick="rateTrk(0,' + trkId + ')" /><img src="./themes/' + themeName + '/img/virtue2_off.png" onClick="rateTrk(1,' + trkId + ')" /><img src="./themes/' + themeName + '/img/virtue3_off.png" onClick="rateTrk(2,' + trkId + ')" /><img src="./themes/' + themeName + '/img/star_selected.png" onClick="rateTrk(3,' + trkId + ')" /><img src="./themes/' + themeName + '/img/star_selected.png" onClick="rateTrk(4,' + trkId + ')" /><img src="./themes/' + themeName + '/img/star_unselected.png" onClick="rateTrk(5,' + trkId + ')" />';
   } else if (trkVirtue == 3) { 
      document.getElementById(trk).innerHTML = '<input type="hidden" name="trk[' + trkId + '][virtue]" value="' + trkVirtue + '" /><img src="./themes/' + themeName + '/img/virtue1_off.png" onClick="rateTrk(0,' + trkId + ')" /><img src="./themes/' + themeName + '/img/virtue2_off.png" onClick="rateTrk(1,' + trkId + ')" /><img src="./themes/' + themeName + '/img/virtue3_off.png" onClick="rateTrk(2,' + trkId + ')" /><img src="./themes/' + themeName + '/img/star_selected.png" onClick="rateTrk(3,' + trkId + ')" /><img src="./themes/' + themeName + '/img/star_unselected.png" onClick="rateTrk(4,' + trkId + ')" /><img src="./themes/' + themeName + '/img/star_unselected.png" onClick="rateTrk(5,' + trkId + ')" />';
   } else if (trkVirtue == 2) {
      document.getElementById(trk).innerHTML = '<input type="hidden" name="trk[' + trkId + '][virtue]" value="' + trkVirtue + '" /><img src="./themes/' + themeName + '/img/virtue1_off.png" onClick="rateTrk(0,' + trkId + ')" /><img src="./themes/' + themeName + '/img/virtue2_off.png" onClick="rateTrk(1,' + trkId + ')" /><img src="./themes/' + themeName + '/img/virtue3_on.png" onClick="rateTrk(2,' + trkId + ')" /><img src="./themes/' + themeName + '/img/star_unselected.png" onClick="rateTrk(3,' + trkId + ')" /><img src="./themes/' + themeName + '/img/star_unselected.png" onClick="rateTrk(4,' + trkId + ')" /><img src="./themes/' + themeName + '/img/star_unselected.png" onClick="rateTrk(5,' + trkId + ')" />';
   } else if (trkVirtue == 1) {
      document.getElementById(trk).innerHTML = '<input type="hidden" name="trk[' + trkId + '][virtue]" value="' + trkVirtue + '" /><img src="./themes/' + themeName + '/img/virtue1_off.png" onClick="rateTrk(0,' + trkId + ')" /><img src="./themes/' + themeName + '/img/virtue2_on.png" onClick="rateTrk(1,' + trkId + ')" /><img src="./themes/' + themeName + '/img/virtue3_off.png" onClick="rateTrk(2,' + trkId + ')" /><img src="./themes/' + themeName + '/img/star_unselected.png" onClick="rateTrk(3,' + trkId + ')" /><img src="./themes/' + themeName + '/img/star_unselected.png" onClick="rateTrk(4,' + trkId + ')" /><img src="./themes/' + themeName + '/img/star_unselected.png" onClick="rateTrk(5,' + trkId + ')" />';
   } else if (trkVirtue == 0) {
      document.getElementById(trk).innerHTML = '<input type="hidden" name="trk[' + trkId + '][virtue]" value="' + trkVirtue + '" /><img src="./themes/' + themeName + '/img/virtue1_on.png" onClick="rateTrk(0,' + trkId + ')" /><img src="./themes/' + themeName + '/img/virtue2_off.png" onClick="rateTrk(1,' + trkId + ')" /><img src="./themes/' + themeName + '/img/virtue3_off.png" onClick="rateTrk(2,' + trkId + ')" /><img src="./themes/' + themeName + '/img/star_unselected.png" onClick="rateTrk(3,' + trkId + ')" /><img src="./themes/' + themeName + '/img/star_unselected.png" onClick="rateTrk(4,' + trkId + ')" /><img src="./themes/' + themeName + '/img/star_unselected.png" onClick="rateTrk(5,' + trkId + ')" />';
   }

*/


// Can't use this ajax method until we figure out a better way to send 
// multiple simultaneous httpRequest() queries.
//   requestQuery = './includes/ajax_func.php?do=rateTrk&trkRow=' + trkRow;
//   if (trkVirtue != null) requestQuery += '&trkVirtue=' + trkVirtue;
//   req.open('get', requestQuery);
//   req.onreadystatechange = handleResponse;
//   req.send(null);





}

function rateTrkAll (trkVirtue, lastTrk) {
   if (lastTrk == null) lastTrk = document.getElementById('lastTrk').value;
   //alert('hey' + lastTrk);
   for (var i=0; i < lastTrk; i++) {
//      var trkVirtueName = 'document.albEdit.trk[' + i + '][virtue]';
//      alert(trkVirtueName.value);
//      if (trkVirtueName.value == 5) {
//         alert('updating track' + i + ' to value ' + trkVirtue +'.  ' + '(last track is ' + lastTrk + ')');
         rateTrk(i, trkVirtue);
//      }
   }
}

function blurAddTrk (trkRow) {
   var elementId = 'trkTitle' + trkRow;
   var elementIdArtist = 'trkArtist' + trkRow;
   if ( (document.getElementById('lastTrk').value == trkRow) && (document.getElementById(elementId).value != '') ) {
      if ( (document.getElementById('is_comp').checked == false) || (document.getElementById(elementIdArtist).value != '') ) {
         addTrk(trkRow + 1);
      }
   }
      document.getElementById(elementIdArtist).focus();
}

function addTrk (trkRow) {
   objRowLength = document.getElementsByTagName("TR").length - 1;
   objLastRow = document.getElementsByTagName("TR").item(objRowLength);
   
   var lastTrk = document.getElementById('lastTrk').value;
//   var 
   
   var newTableBod = document.createElement("TBODY");
   var newTableRow = document.createElement("TR");
   newTableBod.appendChild(newTableRow);
   document.getElementById('artistTable').appendChild(newTableBod);
   var trkId = 'trkRow[' + trkRow + ']';
   newTableRow.setAttribute("id", trkId);
   newTableRow.setAttribute("class", "tracklist");
   
   var newTableBod2 = document.createElement("TBODY");
   var newTableRow2 = document.createElement("TR");
   newTableBod2.appendChild(newTableRow2);
   document.getElementById('artistTable').appendChild(newTableBod2);
   var trkId2 = 'trkExtInfo[' + trkRow + ']';
   newTableRow2.setAttribute("id", trkId2);
   
   var requestQuery = './includes/ajax_func.php?do=addTrk&trkRow=' + trkRow;
   if (!document.getElementById('trkExtInfo').checked) {
      requestQuery += '&extInfo=true';
      newTableRow2.setAttribute("class", "extInfoHidden");
   } else {
      newTableRow2.setAttribute("class", "extInfo");
   }
   if (document.getElementById('is_comp').checked) {
      requestQuery += '&isComp=true';
   }
   if (document.getElementById('albId').value == 'new') {
      requestQuery += '&albId=new';
   }
   
   // check if extended track info is being shown; 
   // hide extended info if appropriate
   if (document.getElementById('trkExtInfo').checked) {
      newTableRow2.className = 'extInfoHidden'; // switchElementClassName('extInfo','extInfoHidden');
   } else {
      newTableRow2.className = 'extInfo';
      switchElementClassName('extInfoHidden','extInfo');
   }
   
   req.open('get', requestQuery);
   req.onreadystatechange = handleResponse;
   req.send(null);
   
   var newLastTrk = ++lastTrk;
   document.getElementById('lastTrk').setAttribute('value', newLastTrk);
//   alert('hey' + newLastTrk);
//   if (document.getElementById('lastTrk').setAttribute("value", newLastTrk )) {
//      alert('it worked: ' + newLastTrk);
//   };
}

// 
// this function is for changing the track number of a track listed in the albEdit mode
// 
function changeTrkNum (trkRow, oldTrkNum) {
   var trkRowIdName = 'trk[' + trkRow + '][trkNum]';
   document.getElementById(trkRowIdName).setAttribute('onClick', '');
   document.getElementById(trkRowIdName).innerHTML = '<input type="text" size="1" name="trk[' + trkRow + '][num]" value="' + oldTrkNum + '" />';
}

//////////////////////////////////////////////////////////////////////////
//
// SECTION progSched
// 
// The following functions are specific to the program schedule editing mode.
// 
//////////////////////////////////////////////////////////////////////////

function addProg () {
   var progStart = document.getElementById('starttime').value;
   alert(progStart);
   //req.open('get', './includes/ajax_func.php?do=addProg');
   //req.onreadystatechange = handleResponse;
   //req.send(null);
}


//////////////////////////////////////////////////////////////////////////
//
// SECTION usr
// 
// The following functions are specific to the user edit/add/search/change-password/etc modes.
// 
//////////////////////////////////////////////////////////////////////////

function changeUsrnamePasswdPrompt () {
   document.getElementById('passwdPrompt').innerHTML = '<input type="password" name="password" value="" autocomplete="off" /> the user password is required to change user name!';
}


//////////////////////////////////////////////////////////////////////////
//
// SECTION statistics
// 
// The following functions are specific to tracking, statistics, and cmj reporting modes.
// 
//////////////////////////////////////////////////////////////////////////


//
// in cmj statistics mode, this function moves an album up on the ranking list by 
// refreshing the list using ajax
//
function cmjMvAlbUp (rowId) {
   var listMax = document.getElementById('cmjListRowLength').value;
   var urlQuery = '';
   for (i=1; i <= listMax; i++) {
      var albIdObj = 'cmj[' + i + '][albId]';
      var spinsObj = 'cmj[' + i + '][spins]';
      var albId = document.getElementById(albIdObj);
      var spins = document.getElementById(spinsObj);
      urlQuery += albId.value + '|' + spins.value;
      urlQuery += '::';
   }
//   alert('cmj mv up: ' + rowId + ' to ' + (rowId - 1) + ' (of ' + listMax + ') query:' + urlQuery);
   req.open('get', './includes/ajax_func.php?do=cmjMvAlbUp&r=' + rowId +'&q=' + urlQuery);
   req.onreadystatechange = handleResponse;
   req.send(null);
}

function cmjTsvExport () {
   var listMax = document.getElementById('cmjListRowLength').value;
   var urlQuery = '';
   for (i=1; i <= listMax; i++) {
      var albIdObj = 'cmj[' + i + '][albId]';
      var spinsObj = 'cmj[' + i + '][spins]';
      var albId = document.getElementById(albIdObj);
      var spins = document.getElementById(spinsObj);
      urlQuery += albId.value + '|' + spins.value;
      urlQuery += '::';
   }
   req.open('get', './includes/ajax_func.php?do=cmjTsvExport&q=' + urlQuery);
   req.onreadystatechange = handleResponse;
   req.send(null);
   
}

</script>


<!-- // begin traffic log expand code -->

<script language="javascript" type="text/javascript">
//this is version 2.0 of the expandAirplay function... widget capable
function expandAirplay (airId) {
	if (document.getElementById('air_trunc[' + airId + ']').className == 'highlighted') {
		document.getElementById('air_trunc[' + airId + ']').className = 'hidden';
		document.getElementById('air_untrunc[' + airId + ']').className = 'highlighted';
	} else if (document.getElementById('air_trunc[' + airId + ']').className == 'hidden') {
		document.getElementById('air_trunc[' + airId + ']').className = 'highlighted';
		document.getElementById('air_untrunc[' + airId + ']').className = 'hidden';
	}
}
</script>


<!-- // end traffic log expand code -->

<!-- //begin traffic check/uncheck request code -->
<script language="javascript" type="text/javascript">
function request(airId) {
   if(document.getElementById('request[' + airId + ']').checked) {   
      //alert("Box is checked"); 
      //document.getElementById('request[' + airId + ']').disabled=true;
   } else { 
      alert("Unchecked"); 
   }
}
</script>
<!-- //end traffic check/uncheck request code -->


<!-- begin nifty calendar script includes; a great lil' GPL date selector courtesy of dynarch.com -->
<style type="text/css">@import url(./themes/<?=$theme?>/jscalendar-1.0/calendar-blue.css);</style>
<script type="text/javascript" src="./includes/jscalendar-1.0/calendar.js"></script>
<script type="text/javascript" src="./includes/jscalendar-1.0/lang/calendar-en.js"></script>
<script type="text/javascript" src="./includes/jscalendar-1.0/calendar-setup.js"></script>

<title>
<?php echo $CALL_LETTERS ?> Music Database</title>
<style type="text/css"><?php
include('./themes/'.$theme.'/default.css');
?></style>
</head>

<body onMouseDown="parent.destroyContext();">

<?php 
$self = $_SERVER[PHP_SELF]
?>

<ul class="topmenu" id="dmenu">
	<li class="topmenuli"><a href="<?=$self?>?mode=home" class="topmenutitle">Home</a>
	   <ul class="submenuul">
		   <li class="submenuli"><a href="<?=$self?>?mode=userchangepassword">change password</a></li>
	   </ul>
	</li>

	<li class="topmenuli"><a href="<?=$self?>?mode=traffic" class="topmenutitle">Program Log</a>
	   <?php 
	   if ($rights > 1) {
	   ?>
	   <ul class="submenuul">
	      <li class="submenuli"><a href="<?=$self?>?mode=stats">statistics</a></li>
	      <li class="submenuli"><a href="<?=$self?>?mode=showaudit">audit show</a></li>
	      <li class="submenuli"><a href="<?=$self?>?mode=cmj">CMJ Report</a></li>
	      <li class="submenuli"><a href="<?=$self?>?mode=cdlistedit">list view</a></li>
	      <li class="submenuli"><a href="<?=$self?>?mode=promoter">promoters</a></li>
	   </ul>
	   <?php 
	   }
	   ?>

	</li>
	<?php 
	if ($rights > 1) {
	?>
	<li class="topmenuli"><a href="<?=$self?>?mode=cdlistedit" class="topmenutitle">Albums</a>
   	<ul class="submenuul">
	   	<li class="submenuli"><a href="<?=$self?>?mode=cdlistedit">list albums</a></li>
   		<li class="submenuli"><a href="<?=$self?>?mode=albEdit&albId=new">add album</a></li>
	   	<li class="submenuli"><a href="<?=$self?>?mode=rotationImport">add to rotation</a></li>
		   <li class="submenuli"><a href="<?=$self?>?mode=category">edit categories</a></li>
		   <li class="submenuli"><a href="<?=$self?>?mode=scanTrkFiles">import music from files</a></li>
		   <li class="submenuli"><a href="<?=$self?>?mode=collectionprint">print collection</a></li>
		   <li class="submenuli"><a href="<?=$self?>?mode=labelprint">print labels</a></li>
   	</ul>
	</li>
	<li class="topmenuli"><a href="<?=$self?>" class="topmenutitle">Schedule</a>
   	<ul class="submenuul">
	   	<li class="submenuli"><a href="<?=$self?>?mode=trafSched">traffic schedule</a></li>
		   <li class="submenuli"><a href="<?=$self?>?mode=progSched">program schedule</a></li>
   		<li class="submenuli"><a href="<?=$self?>?mode=usr">edit users</a></li>
   		<li class="submenuli"><a href="<?=$self?>?mode=usrAdd">add user</a></li>
   		<li class="submenuli"><a href="<?=$self?>?mode=evt">edit/add event types</a>
         <li class="submenuli"><a href="<?=$self?>?mode=scanEvtFiles">import events from files</a></li>   		
   	</ul>
	</li>
	<?php 
	}
	?>
	<li class="topmenuli"><a href="" class="topmenutitle">Options</a>
   	<ul class="submenuul">
	   	<li class="submenuli"><a href="<?=$self?>?mode=mkMagicPlaylist">Make Magic Playlist</a></li>
	   	<li class="submenuli"><a href="<?=$self?>?mode=mkPlaylist">Auto-Playlist</a></li>
	   	<li class="submenuli"><a href="<?=$self?>?mode=mkManPlaylist">Make Manual Playlist</a></li>
	   	<li class="submenuli"><a href="<?=$self?>?mode=dbCheck">Database Check</a></li>
   	</ul>
	</li>
</ul>


<div class="content">
<?php 

if ($_REQUEST[loading]) {
   // include the "loading..." widget
   include('./widgets/loading1.inc');
}

?>

<div style="position:absolute; right: 1px; top: 1px; width: 40px; height: 34px; text-align: right"><a href="?mode=pre_logout">logout</a></div>
<table border="0" cellpadding="0" cellspacing="0"><tr>
   <td valign="top">
   


<form name="topsearch" action="<?=$_SERVER[PHP_SELF]?>" method="post" ><input type="hidden" name="cat" value="<?php echo($cat) ?>" /><input type="hidden" name="mode" value="search" /><input type="text" name="search_for"  onblur="if (value == '') {value = 'Search'}" onfocus="if (value == 'Search') {value =''}" value="Search" size="10" /></form>

   <div style="position: absolute; top: 36px; left:0px">
<br />
<center><span id="alertMessage" style="color:red; background-color: white;"></span></center>
<br />

<table style="margin-left: 8px; margin-top: 6px;">
<tr>
<td width="8"><img width="8" height="8" src="./themes/<?=$theme?>/img/top_left_corner.png" border="0" alt="" /></td>
<td style="background-image: url(./themes/<?=$theme?>/img/top_middle_bg.png);"><img alt="" border="0" width="10" height="8" src="./themes/<?=$theme?>/img/top_middle.png" /></td>
<td width="8"><img width="8" height="8" src="./themes/<?=$theme?>/img/top_right_corner.png" border="0" alt="" /></td>
</tr>
<tr>
<td width="8" background="./themes/<?=$theme?>/img/left_middle_bg.png"><img height="10" alt="" src="./themes/<?=$theme?>/img/left_middle.png" width="8" border="0" /></td>
<td>
<!-- other available modes: m3umake, mkPlaylist2, slotcheck, cdmassedit, importFiles, adddateimport -->
<?php

switch($mode) {
default:
   include ('./mode/traffic.inc');
   break;
case 'traffic':
   include ('./mode/traffic.inc');
   break;
case 'home':
   include('./mode/home.inc');
   break;
case 'scanTrkFiles':
   include('./mode/scanTrkFiles.inc');
   break;
case 'scanEvtFiles':
   include('./mode/scanEvtFiles.inc');
   break;
case 'importFiles':
   include ('./testing/test_loader.php');
   break;
case 'search':
	include ('./mode/search.inc');
	break;
case 'cdedit':
	include ('./mode/cdedit.inc');
	break;
case 'albEdit':
   include ('./mode/cdedit.inc');
   break;
case 'cdadd':
	include ('./mode/cdadd.inc');
	break;
//case 'trafSched':
	//include ('./mode/sched_add.inc');
	//break;
case 'progSched':
   include ('./mode/progSched.inc');
   break;
case 'cdview':
	include ('./mode/cdview.inc');
	break;
case 'stats':
	include ('./mode/stats.inc');
	break;
case 'cdlist':
	include ('./mode/cdlist.inc');
	break;
case 'cdmassedit':
	include ('./mode/cdmassedit.inc');
	break;
case 'cdlistedit':
	include ('./mode/cdlistedit.inc');
	break;
case 'slotcheck':
	include ('./mode/slotcheck.inc');
	break;
case 'cmjexport':
	include ('./mode/cmj_export.inc');
	break;
case 'cmj':
	include ('./mode/cmj.inc');
	break;
case 'showaudit':
	include ('./mode/show_audit.inc');
	break;
case 'labelprint':
	include ('./mode/labelprint.inc');
	break;
case 'collectionprint':
	include ('./mode/collectionprint.inc');
	break;
case 'mkPlaylist2':
   include('./mode/mkPlaylist2.inc');
//   include ('./mode/mkPlaylist.inc');
   break;
case 'mkPlaylist':
   include('./mode/mkPlaylist.inc');
//   include ('./mode/mkPlaylist.inc');
   break;
case 'mkManPlaylist':
   include('./mode/mkManPlaylist.inc');
   break;
case 'albImport':
   include ('./mode/albImport.inc');
   break;
case 'albDel':
   if ($rights > 2) include ('./mode/albDel.inc');
   else echo('Sorry!  You don\'t have permission to do that!');
   break;
case 'adddateimport':
	include ('./mode/adddateimport.inc');
	break;
case 'promoter':
	include ('./mode/promoter.inc');
	break;
case 'promoSrch':
   include ('./mode/promoter.inc');
   break;
case 'promoEdit':
   include ('./mode/promoter.inc');
   break;
case 'usr':
	include ('./mode/user.inc');
	break;
case 'usrAdd':
   include ('./mode/user.inc');
   break;
case 'editUsr':
   include ('./mode/user.inc');
   break;
case 'usrSrch':
   include ('./mode/user.inc');
   break;
case 'userchangepassword':
	include ('./mode/userchangepassword.inc');
	break;
case 'pre_logout':
	include ('./mode/pre_logout.inc');
	break;
case 'm3umake':
	include ('./mode/m3u_make.inc');
	break;
case 'trafSched':
	include ('./mode/trafSched.inc');
	break;
case 'rotationImport':
	include ('./mode/rotationImport.inc');
	break;
case 'evt':
   include('./mode/event.inc');
   break;
case 'mkMagicPlaylist':
   include('./mode/mkMagicPlaylist.inc');
   break;
case 'category':
	include ('./mode/category.inc');
	break;
case 'dbCheck':
   include ('./mode/dbCheck.inc');
   break;
}//end if inclusions for different modes
?>
</td><td style="background-image: url(./themes/<?=$theme?>/img/right_middle.png);"></td></tr>
<tr>
<td width="8"><img width="8" height="8" src="./themes/<?=$theme?>/img/bottom_left_corner.png" border="0" alt="" /></td>
<td style="background-image: url(./themes/<?=$theme?>/img/bottom_middle_bg.png);"></td>
<td width="8"><img width="8" height="8" src="./themes/<?=$theme?>/img/bottom_right_corner.png" border="0" alt="" /></td>
</tr>
</table>

<?php 
if ($_REQUEST[loading]) {
   // include the "loading..." widget shutoff trigger 
   include ('./widgets/loading2.inc');
}
?>
</td></tr></table>
</div>

<iframe id="iframeContext" src="./widgets/blank.inc" class="hidden"></iframe>
</body>
</html>
<?php
mysql_close($view_connect);

//
//
//
//
//
//
//
//
//} else { //if there was no $U_id after all
//	require("./login.php");
//}
//
//
//
//
//


?>